package com.luchao.back.controller;

import com.luchao.back.common.constant.Execute;
import com.luchao.back.common.constant.Validate;
import com.luchao.back.common.pojo.PageResult;
import com.luchao.back.common.pojo.ResponseResult;
import com.luchao.back.pojo.Brands;
import com.luchao.back.service.BrandsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "品牌管理")
@RestController
@RequestMapping("/brands")
public class BrandsController {

    @Autowired
    private BrandsService brandsService;

    @ApiOperation("分页获取品牌信息")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页码", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页长度", required = false, paramType = "query", dataType = "Integer", dataTypeClass = Integer.class, defaultValue = "5")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getBrandsByPage")
    public ResponseResult getBrandsByPage(Integer pageNum, Integer pageSize) {
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 5 : pageSize;
        long total = brandsService.getBrandsCount();
        long totalPages = total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1;
        pageNum = total <= pageSize ? 1 : pageNum;
        PageResult<Brands> pageResult = new PageResult<Brands>();
        pageResult.setPageNum(pageNum);
        pageResult.setCount(total);
        pageResult.setPage(totalPages);
        pageResult.setResult(brandsService.getBrandsByPage(pageNum, pageSize));
        return ResponseResult.success(pageResult);
    }

    @ApiOperation("添加品牌")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "brands", value = "品牌对象信息", required = true, paramType = "formData", dataType = "Brands", dataTypeClass = Brands.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/addBrands")
    public ResponseResult addBrands(@RequestBody Brands brands) {
        return brandsService.addBrands(brands) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "新增成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "新增失败", Execute.ERROR);
    }

    @ApiOperation("修改品牌")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "brands", value = "品牌对象信息", required = true, paramType = "formData", dataType = "Brands", dataTypeClass = Brands.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/editBrands")
    public ResponseResult editBrands(@RequestBody Brands brands) {
        return brandsService.editBrands(brands) > 0 ? ResponseResult.success(ResponseResult.SUCCESS, "修改成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.FAILED, "修改失败", Execute.ERROR);
    }

    @ApiOperation("校验品牌名称唯一性")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "bname", value = "品牌名称", required = true, paramType = "query", dataType = "String", dataTypeClass = String.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/checkBrands")
    public ResponseResult checkBrands(String bname) {
        return brandsService.checkBrands(bname) > 0 ? ResponseResult.success(ResponseResult.FAILED, "品牌名称已存在,不可以使用", Validate.ORDINARY) : ResponseResult.success(ResponseResult.SUCCESS, "品牌名称不存在,可以使用", Validate.UNIQUE);
    }

    @ApiOperation("批量禁用/解禁品牌")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "list", value = "品牌列表", required = true, paramType = "formData", dataType = "List<Brands>", dataTypeClass = List.class, defaultValue = "")})
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/batchChangeState")
    public ResponseResult batchChangeState(@RequestBody List<Brands> list) {
        return brandsService.batchChangeState(list) ? ResponseResult.success(ResponseResult.SUCCESS, "操作成功", Execute.SUCCESS) : ResponseResult.success(ResponseResult.ERROR, "操作失败", Execute.ERROR);
    }

    @ApiOperation("获取所有品牌信息")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllBrands")
    public ResponseResult getAllBrands() {
        return ResponseResult.success(brandsService.getAllBrands());
    }

    @ApiOperation("获取所有前台展示的品牌信息")
    @ApiResponses(value = @ApiResponse(description = "code:响应状态码,message:返回操作消息,data:操作携带的具体数据信息"))
    @PostMapping("/getAllBrandsForView")
    public ResponseResult getAllBrandsForView() {
        return ResponseResult.success(brandsService.getAllBrandsForView());
    }
}
